package someTestExcemple.thread.communication;

import java.util.ArrayList;
import java.util.List;

public class Desk {
    private volatile List<String> list = new ArrayList<String>();
    //放包子
    public synchronized void put() {
        String name = Thread.currentThread().getName();
        //判断是否有包子
        try {
            if (list.size() <= 10) {
                list.add(name + "做的肉包子");
                System.out.println(name + "把做好的肉包子放在桌子上");
                Thread.sleep(2000);
                //再等待自己，先唤醒别人 --千万别反 先自己等待了，怎么唤醒别人
                this.notifyAll();
                this.wait();
            } else {
                //有包子-把做了
                //唤醒别人 等待自己
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void get() {
        try {
            if(list.size() >=5) {
                String name = Thread.currentThread().getName();
                System.out.println(name + " 吃了" + list.get(0));
                list.clear();
                Thread.sleep(3000);
                this.notifyAll();
                this.wait();
            } else {
                this.notifyAll();
                this.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
